package org.jeecg.modules.demo.checkOrder.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.checkOrder.entity.CheckOrderRk;
import org.jeecg.modules.demo.checkOrder.entity.checkOrder;
import org.jeecg.modules.demo.checkOrder.mapper.CheckOrderRkMapper;
import org.jeecg.modules.demo.checkOrder.mapper.checkOrderMapper;
import org.jeecg.modules.demo.checkOrder.service.IcheckOrderService;
import org.jeecg.modules.demo.soOrderDetail.entity.SoOrderDetail;
import org.jeecg.modules.demo.soOrderDetail.mapper.SoOrderDetailMapper;
import org.jeecg.modules.demo.utils.BigDecimalUtil;
import org.jeecg.modules.demo.utils.generateOddUtil;
import org.jeecg.modules.demo.warehouseorder.entity.WarehouseOrder;
import org.jeecg.modules.demo.warehouseorder.mapper.WarehouseOrderMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.io.Serializable;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Description: checkOrder
 * @Author: jeecg-boot
 * @Date:   2022-01-19
 * @Version: V1.0
 */
@Service
public class checkOrderServiceImpl extends ServiceImpl<checkOrderMapper, checkOrder> implements IcheckOrderService {

	@Resource
	private checkOrderMapper checkOrderMapper;
	@Resource
	private CheckOrderRkMapper checkOrderRkMapper;
	@Resource
	private WarehouseOrderMapper warehouseOrderMapper;
	@Resource
	private SoOrderDetailMapper soOrderDetailMapper;

	
	@Override
	@Transactional
	public void delMain(String id) {
		checkOrderRkMapper.deleteByMainId(id);
		checkOrderMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			checkOrderRkMapper.deleteByMainId(id.toString());
			checkOrderMapper.deleteById(id);
		}
	}

	@Override
	public String getMaxCode(){
		return checkOrderMapper.getMaxCode();
	}

	@Override
	public checkOrder getByPdNo(String str){
		return checkOrderMapper.getByPdNo(str);
	}

	@Override
	@Transactional
	public boolean deleteOrder(checkOrder checkOrder){
		try{
			checkOrderMapper.deleteById(checkOrder.getId());
			checkOrderRkMapper.deleteByMainId(checkOrder.getPdNo());
			return true;
		}catch (Exception e){
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return false;
		}
	}


	/**
	 * 添加
	 * @param checkOrder
	 * @param orderRks
	 * @return
	 */
	@Override
	@Transactional
	public Boolean addCheckOrderAndRk(checkOrder checkOrder, List<CheckOrderRk> orderRks){
		try {
			checkOrderMapper.insert(checkOrder);
			for (CheckOrderRk orderRk : orderRks) {
				orderRk.setCreateTime(new Date());
				orderRk.setPdDate(new Date());
				checkOrderRkMapper.insert(orderRk);
			}
			return true;
		}catch (Exception e){
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return false;
		}
	}

	/**
	 * 审核
	 * @param checkOrder
	 * @param orderRks
	 * @return
	 */
	@Override
	@Transactional
	public boolean check(checkOrder checkOrder,List<CheckOrderRk> orderRks,String precode){
		try {
			String resultCode = getRkCode(precode);
			String ckCode = getCKCode(precode);
			int a = Integer.parseInt(resultCode.substring(resultCode.length()-1));
			int b = Integer.parseInt(ckCode.substring(ckCode.length()-1));
			String wono = resultCode.substring(0,resultCode.length()-1);
			String sodNo = ckCode.substring(0,ckCode.length()-1);
			for (CheckOrderRk orderRk : orderRks) {
				String ck = sodNo+b;
				String rk = wono+ a;
				WarehouseOrder warehouseOrder = warehouseOrderMapper.getByWoNo(orderRk.getWoNo());
				if(orderRk.getProTon().compareTo(BigDecimal.ZERO)==0){
					continue;
				}
				//盈亏数量为负数，则盘亏，生成出库单
				if(orderRk.getProTon().compareTo(BigDecimal.ZERO)<0){
					soOrderDetailMapper.insert(createSoOrderDetail(orderRk,ck));
					warehouseOrder.setStandingstock(orderRk.getRealQu());
					warehouseOrderMapper.updateById(warehouseOrder);
					b++;
				}else if(orderRk.getWoNo()==null){
					warehouseOrderMapper.insert(createWareHouseOrder(orderRk,rk));
					a++;
				}else {
					warehouseOrder.setStandingstock(orderRk.getRealQu());
					warehouseOrderMapper.updateById(warehouseOrder);
					warehouseOrderMapper.insert(createWareHouseOrder(orderRk,rk));
					a++;
				}
				orderRk.setShenhe(1);
				checkOrderRkMapper.updateById(orderRk);
			}
			// 获取登录用户信息
			LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
			checkOrder.setShenhe(1);
			checkOrder.setSaleman(sysUser.getId());
			checkOrder.setPdTime(new Date());
			checkOrderMapper.updateById(checkOrder);
			return true;
		}catch (Exception e){
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return false;
		}
	}

	/**
	 * 弃审
	 * @param checkOrder
	 * @return
	 */
	@Override
	@Transactional
	public boolean unCheck(checkOrder checkOrder){
		try{
			List<CheckOrderRk> orderRks = checkOrderRkMapper.getByPdNo(checkOrder.getPdNo());
			for (CheckOrderRk orderRk : orderRks) {
				WarehouseOrder warehouseOrder = warehouseOrderMapper.getByWoNo(orderRk.getWoNo());
				if(orderRk.getProTon().compareTo(BigDecimal.ZERO)==0){
					continue;
				}
				if(orderRk.getWoNo()==null){
					orderRk.setShenhe(0);
					checkOrderRkMapper.updateById(orderRk);
					continue;
				}
				warehouseOrder.setStandingstock(orderRk.getPhyCq());
				warehouseOrderMapper.updateById(warehouseOrder);
				orderRk.setShenhe(0);
				checkOrderRkMapper.updateById(orderRk);
			}
			checkOrder.setShenhe(0);
			checkOrderMapper.updateById(checkOrder);
			warehouseOrderMapper.deleteByPdNo(checkOrder.getPdNo());
			soOrderDetailMapper.deleteByPdNo(checkOrder.getPdNo());
			return true;
		}catch (Exception e){
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return false;
		}
	}

	/**
	 * 其他入库单号bug解决
	 * @return
	 */
   public String getRkCode(String precode){
	   String str = warehouseOrderMapper.getMaxCode();
	   Map<String,String> map = new HashMap<>();
	   if(str==null){
		   map.put("maxCode",null);
	   }else{
		   map.put("maxCode",str);
	   }
	   String name = precode+"QT_";
	   String resultCode = generateOddUtil.createCode(map,name);
	   return resultCode;
   }

	/**
	 * 其他出库单号bug解决
	 * @return
	 */
	public String getCKCode(String precode){
	   String str = soOrderDetailMapper.getMaxCode();
	   Map<String,String> map = new HashMap<>();
	   if(str==null){
		   map.put("maxCode",null);
	   }else{
		   map.put("maxCode",str);
	   }
	   String name = precode+"QT_";
	   String resultCode = generateOddUtil.createCode(map,name);
	   return resultCode;
   }

	/**
	 * 生成其他出库单
	 * @param orderRk
	 * @return
	 */
	public SoOrderDetail createSoOrderDetail(CheckOrderRk orderRk,String str){
		SoOrderDetail soOrderDetail = new SoOrderDetail();
		soOrderDetail.setSodNo(str);
		Double d = -1.00;
		Double b = orderRk.getProTon().doubleValue();
		Double c = orderRk.getProLoss().doubleValue();
		soOrderDetail.setSodQuantity(new BigDecimal(BigDecimalUtil.mul(d,b)));
		soOrderDetail.setXclNum(new BigDecimal(BigDecimalUtil.mul(d,b)));
		soOrderDetail.setIsQuality(0);
		soOrderDetail.setSodDate(new Date());
		soOrderDetail.setCreateTime(new Date());
		soOrderDetail.setWhNum(orderRk.getWhNum());
		soOrderDetail.setPuWhNum(orderRk.getWhNum());
		soOrderDetail.setSodStatus(0);
		soOrderDetail.setIsDelete(0);
		soOrderDetail.setPrdNo(orderRk.getCinvname());
		soOrderDetail.setJldw(orderRk.getMeasureunit());
		soOrderDetail.setPrice(orderRk.getAveAmount().doubleValue());
		soOrderDetail.setWsdj(orderRk.getAveAmount());
		soOrderDetail.setWsje(new BigDecimal(BigDecimalUtil.mul(d,b)));
		soOrderDetail.setCkType("2");
		soOrderDetail.setSoStaff(orderRk.getPoStaff());
		soOrderDetail.setDept(orderRk.getDepartment());
		soOrderDetail.setCbJine(new BigDecimal(BigDecimalUtil.mul(d,c)));
		soOrderDetail.setPdNo(orderRk.getPdNo());
		soOrderDetail.setShName(orderRk.getSaleman());
		soOrderDetail.setCreateBy(orderRk.getCreateBy());
		soOrderDetail.setSysOrgCode(orderRk.getSysOrgCode());
		return soOrderDetail;
	}

	/**
	 * 生成其他入库单
	 * @param orderRk
	 * @return
	 */
	public WarehouseOrder createWareHouseOrder(CheckOrderRk orderRk,String str){
		WarehouseOrder warehouseOrder = new WarehouseOrder();
		warehouseOrder.setWoNo(str);
		warehouseOrder.setPuWoNo(str);
		warehouseOrder.setProWhNum(orderRk.getWhNum());
		warehouseOrder.setWhNum(orderRk.getWhNum());
		warehouseOrder.setQoQuantity(orderRk.getProTon());
		warehouseOrder.setWoDate(new Date());
		warehouseOrder.setCreateTime(new Date());
		warehouseOrder.setPuWhNum(orderRk.getWhNum());
		warehouseOrder.setShenhe(0);
		warehouseOrder.setShanchu(0);
		warehouseOrder.setPoTotail(orderRk.getProTon());
		warehouseOrder.setFtwsdj(orderRk.getAveAmount());
		warehouseOrder.setWsdj(orderRk.getAveAmount());
		warehouseOrder.setFtwsje(orderRk.getProLoss());
		warehouseOrder.setWsje(orderRk.getProLoss());
		warehouseOrder.setTotalcost(orderRk.getProLoss());
		warehouseOrder.setStandingstock(orderRk.getProTon());
		warehouseOrder.setMeasureunit(orderRk.getMeasureunit());
		warehouseOrder.setDepartment(orderRk.getDepartment());
		warehouseOrder.setPoStaff(orderRk.getPoStaff());
		warehouseOrder.setRklb(2);
		warehouseOrder.setCinvname(orderRk.getCinvname());
		warehouseOrder.setPdNo(orderRk.getPdNo());
		warehouseOrder.setSaleman(orderRk.getSaleman());
		warehouseOrder.setCreateBy(orderRk.getCreateBy());
		warehouseOrder.setSysOrgCode(orderRk.getSysOrgCode());
		return warehouseOrder;
	}

	@Override
	public List<checkOrder> getByWhNum(String str){
		return checkOrderMapper.getByWhNum(str);
	}

	@Override
	public List<WarehouseOrder> getWareHouseOrderByPdNo(String str){
		return checkOrderMapper.getWareHouseOrderByPdNo(str);
	}

	@Override
	public List<SoOrderDetail> getSoOrderDetailsByPdNo(String str){
		return checkOrderMapper.getSoOrderDetailsByPdNo(str);
	}
	
}
